﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CPGE
{
    public partial class Components_RootBloc : System.Web.UI.UserControl
    {
        public string Titre { get; set; }
        public string ItemTitle { get; set; }
        public string SectionName{ get; set; }
        public bool ShowTitle { get; set; }
        public bool WithLink = true;
        public bool WithNav { get; set; }
        public BlocManager.BlocType Type;
        public BlocManager.BlocWidth Size=BlocManager.BlocWidth.Small;

        protected void Page_Load(object sender, EventArgs e)
        {
            
            SetTitle();
            Bloc.Attributes["class"] = "Bloc "+ Size.ToString();
            MainBloc.Attributes["class"] = "BlocMainContent" + Size.ToString();
            MainContent.Controls.Add(GetContent());
        }

        private Control GetContent()
        {
            try
            {
                switch (Type)
                {
                    case BlocManager.BlocType.Actus:
                        if (ItemTitle == null)
                        {
                            
                            return BlocManager.GetArticle(Size, SectionName);
                        }
                        else
                        {
                            return BlocManager.GetArticle(Size, SectionName, ItemTitle);
                        }


                    case BlocManager.BlocType.Focus:
                        if (ItemTitle != null)
                        {
                            return BlocManager.GetFocus(Size, SectionName, ItemTitle);
                        }
                        else throw new Exception();

                    case BlocManager.BlocType.ImgTxt:

                        return BlocManager.GetImgtxt(Size, SectionName, ItemTitle, WithLink);

                    case BlocManager.BlocType.Liens:
                        return BlocManager.GetLinks(Size, SectionName);

                    case BlocManager.BlocType.Menu:
                        return BlocManager.GetMenu(Size, SectionName);

                    case BlocManager.BlocType.Texte:
                        return BlocManager.GetTexte(Size, SectionName, ItemTitle, WithLink);

                    default: return null;
                }
            }
            catch (Exception exc)
            {
                Label lblErr = new Label();
                lblErr.Text = "Problème de récupération de données:" +exc.Message;
                return lblErr;
            }
        }

        private void SetTitle()
        {
            if (!ShowTitle)
            {
                BlocTitle.Visible = false;
            }
            else
            {
                switch (Type)
                {
                    case BlocManager.BlocType.Actus:
                        if (Titre == null)
                            this.Title.Text = "À la une";
                        else
                            this.Title.Text = Titre;
                        break;
                    case BlocManager.BlocType.Focus:
                        if (Titre == null)
                            this.Title.Text = "Le focus sur...";
                        else
                            this.Title.Text = Titre;
                        break;
                    case BlocManager.BlocType.ImgTxt:
                        this.Title.Text = Titre;
                        break;

                    case BlocManager.BlocType.Liens:
                        this.Title.Text = Titre;
                        break;

                    case BlocManager.BlocType.Menu:
                        if (Titre == null)
                            this.Title.Text = "Menu";
                        else
                            this.Title.Text = Titre;
                        break;
                    case BlocManager.BlocType.Texte:
                        this.Title.Text = Titre;
                        break;
                }
            }
        }

        

    }
}
